import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
// 解决ElementUI导航栏中的vue-router重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
var router = new VueRouter({
  routes: [{
    path: '/',
    name: 'logo',
    component: () => import('../views/pages1/logo.vue')
  }, {
    path: '/layout',
    name: 'Layout',
    // hidden:true,
    // redirect:'/layout/Home',
    redirect: {
      name: 'Home'
    },
    meta: {
      title: '首页',
      icon: 'el-icon-house'
    },
    // leafThree:true,//有三级路由
    component: () => import('../views/layout'),
    children: [{
      path: 'Home',
      name: 'Home',
      component: () => import('../views/pages2/Home.vue'),
      meta: {
        title: '首页',
        icon: 'el-icon-house'
      },
    }, {
      path: 'product',
      name: 'product',
      component: () => import('../views/pages3/product.vue'),
      meta: {
        title: '商品',
        icon: 'el-icon-goods'
      },
      children: [{
        path: 'productList',
        name: 'productList',
        component: () => import('../views/pages3/list'),
        meta: {
          title: '商品列表',
          icon: 'el-icon-menu'//el-icon-circle-plus
        },
      }, {
        path: 'productAdd',
        name: 'productAdd',
        component: () => import('../views/pages3/add'),
        meta: {
          title: '商品添加',
          icon: 'el-icon-circle-plus'//el-icon-circle-plus
        },
      }]
    }, {
      path: 'orders',
      name: 'Orders',
      component: () => import('../views/pages4'),
      meta: {
        title: '订单',
        icon: 'el-icon-s-order'
      },
    }, {
      path: 'marketing',
      name: 'Marketing',
      component: () => import('../views/pages5'),
      meta: {
        title: '营销',
        icon: 'el-icon-camera'
      },
    }, {
      path: 'permissions',
      name: 'Permissions',
      component: () => import('../views/pages6'),
      meta: {
        title: '权限',
        icon: 'el-icon-s-grid'
      },
    }]
  }]
})
//befcreEach 前置钩子， afterEach后置钩子
router.beforeEach((to, from, next) => {
  // if(flag == "false")
  // to.name != "login" 
  // next('/login');
  // else{
  //   next();
  // }
  // vuex flag = false;
  // created (){

  // }
  // cookie.get('Flag') != 'fasle' 
  // if(to.name != 'logo' && ()){
  //   next("/");
  // }
  // else{
  //   next();
  // }


  //未登录 : home ==>login
  //to:目标路由，from：起始路由 next()放行   next('/login') next({name:'Login'}) 重定向其他路由
  //获取cookie中缓存的登录状态
  //判断登录状态是否已经已登录

  //不是登录状态
  //  if(to.name=='Login'){
  //   next();
  //   return;
  //  }
  //  if(登录状态){
  //   next();
  //  }else{
  //   next('/login')
  //  }
  //如果目标路由不是Login并且不是登录状态，跳到login 否则放行
  if (to.name != 'logo' && Vue.prototype.$JsCookie.get('name') != '3') {
    next('/')
    return;
  } else {
    next()
  }
})
export default router